const mysql = require('mysql');
const conf = require("../../conf/db")
const pool = mysql.createPool(conf.mysql);
const common = require("../../common");

module.exports = {
    list_all_tasks: (req, res) => {
        pool.getConnection(function (err, connection) {
            if (err) {
                logger.error(err);
            }
            connection.query("select * from task", function (e, result) {
                if (e) {
                    logger.error(e);
                }
                if (result.length > 0) {
                    common.jsonWrite(res, result);
                }
                connection.release();
            })
        })
    },

    read_a_task: (req, res) => {
        pool.getConnection(function (err, connection) {
            if (err) {
                logger.error(err);
            }
            connection.query("select * from task where id = (?)", req.params.id, function (e, result) {
                if (e) {
                    logger.error(e);
                }
                if (result.length > 0) {
                    common.jsonWrite(res, result[0]);
                }
                connection.release();
            })
        })
    }
}
